package me.leetcodepractice;

public class E03Leetcode19 {
    public ListNode remove(ListNode head, int n) {
        // 哨兵节点，为了能删除第一个节点
        ListNode s = new ListNode(-1,head);
        recursion(s,n);
        return s.next;
    }

    private int recursion(ListNode p, int n) {
        if (p == null) {
            return 0;
        }
        // 下一个节点的倒数位置
        int nth = recursion(p.next, n);
        if (nth == n) {
            p.next = p.next.next;
        }
        return nth + 1;
    }

    public static void main(String[] args) {
        ListNode head = ListNode.of(1, 2, 6, 3, 6);
        System.out.println(head);
        System.out.println(new E03Leetcode19().remove(head, 2));
    }
}
